import { sys } from "cc";
import { ServerConfig } from "../../../src/config/ServerConfig";
import HttpUtil from "../../../src/engine/HttpUtil";
import LocalStorage from "../../../src/engine/LocalStorage";
import ServerTimer from "../../../src/engine/ServerTimer";
import { UIManager } from "../../../src/engine/UIManager";
import { NativeSdkHelper } from "../../../src/sdk/NativeSdkHelper";
import { Md5Helper } from "../../../src/engine/Md5Helper";
import { Logger } from "../../../src/engine/Logger";
import { ErrorCode } from "../../../src/config/ErrorCode";
import { AccountModel } from "../login/model/AccountModel";
import { ViewTip } from "../../../src/view/ViewTip";
import { HallSocketController } from "./HallSocketController";

export class HallHttpController {


    public static account_auth(account: string, password: string, callback?: Function) {
        UIManager.instance.showLoadingIcon()
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getAccountServerUrl() + "/account_auth"
        let data: object = {
            "t": ServerTimer.now,
            "account": account,
            "password": password,
            "channel": NativeSdkHelper.channel,
            "os": sys.os,
            "phone_type": NativeSdkHelper.getPhoneType(),
            "bundle_name": NativeSdkHelper.getBundleName(),
            "system_version": NativeSdkHelper.getSystemVersion(),
            "udid": NativeSdkHelper.getUDID()
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("account_auth=req=", data)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            // Logger.log("account_auth=req=222", response)
            UIManager.instance.hideLoadingIcon()
            if (isOK) {
                Logger.log("account_auth==", response)
                if (response) {
                    let code: number = response["code"];
                    if (code == ErrorCode.Code.Suc) {
                        let data: object = response["data"]
                        AccountModel.instance.account = data["account"]
                        AccountModel.instance.nickname = data["nickname"]
                        AccountModel.instance.password = password
                        AccountModel.instance.token = data["token"]
                        AccountModel.instance.save()
                        ServerConfig.initHallServer(data)
                        HallSocketController.instance.login(true);
                        callback && callback(true)
                    } else {
                        let msg: string = response["msg"]
                        UIManager.instance.showTip(msg, ViewTip.TipType.Fail);
                    }
                } else {

                }
            }
        }, JSON.stringify(param));
    }

    // public static wechat_auth(code: string) {
    //     UIManager.instance.showLoadingIcon()
    //     let http: HttpUtil = new HttpUtil();
    //     let url: string = ServerConfig.getAccountServerUrl() + "/wechat_auth"
    //     let data: object = {
    //         "t": DateUtil.now(),
    //         "code": code,
    //         "channel": NativeSdkHelper.channel,
    //         "os": cc.sys.os,
    //         "phone_type": NativeSdkHelper.getPhoneType(),
    //         "bundle_name": NativeSdkHelper.getBundleName(),
    //         "system_version": NativeSdkHelper.getSystemVersion(),
    //         "udid": NativeSdkHelper.getUDID()
    //     }
    //     let param: object = {
    //         "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
    //         "data": data
    //     }
    //     http.setMethod(HttpUtil.methodTypeOption.POST)
    //     http.requestByBody(url, (isOK: boolean, response: any) => {
    //         UIManager.instance.hideLoadingIcon()
    //         if (isOK) {
    //             Logger.log("wechat_auth==", response)
    //             let code: number = response["code"];
    //             if (code == ErrorCode.Code.Suc) {
    //                 let data: object = response["data"];
    //                 AccountModel.instance.account = data["account"]
    //                 AccountModel.instance.token = data["token"]
    //                 AccountModel.instance.save()
    //                 ServerConfig.initHallServer(data)
    //                 SocketClient.login(true)
    //             } else {
    //                 let msg: string = response["msg"]
    //                 UIHelper.showTip(msg)
    //             }
    //         }
    //     }, JSON.stringify(param));
    // }


    public static accountReg(account: string, password: string, callback: Function) {
        UIManager.instance.showLoadingIcon()
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getAccountServerUrl() + "/register"

        let data: object = {
            "t": ServerTimer.now,
            "account": account,
            "password": password,
            "channel": NativeSdkHelper.channel,
            "os": sys.os,
            "phone_type": NativeSdkHelper.getPhoneType(),
            "bundle_name": NativeSdkHelper.getBundleName(),
            "system_version": NativeSdkHelper.getSystemVersion(),
            "udid": NativeSdkHelper.getUDID()
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            UIManager.instance.hideLoadingIcon()
            if (isOK) {
                Logger.log("accountReg==", response)
                if (response) {
                    let code: number = response["code"];
                    if (code == ErrorCode.Code.Suc) {
                        let data: object = response["data"];
                        AccountModel.instance.account = data["account"]
                        AccountModel.instance.token = data["token"]
                        ServerConfig.initHallServer(data)
                        callback && callback(true)
                        HallSocketController.instance.login(true);
                    } else {
                        let msg: string = response["msg"]
                        UIManager.instance.showTip(msg, ViewTip.TipType.Fail)
                        callback && callback(false)
                    }
                } else {

                }
            }
        }, JSON.stringify(param));

    }

}